package cn.util;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.ScanLoaderTask;

public class ScanLoaderUtil {


    final static ExecutorService sExecutors =  Executors.newSingleThreadExecutor();

    public static void initClassPath(ScanLoaderTask classLoaderFindTask) {
        Future<Boolean> future = sExecutors.submit(classLoaderFindTask);
        try {
            Boolean isOk = future.get();
            System.out.println("ScanLoaderUtil: class path 加载完成");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static boolean canInclude(String absolutePath) {
        if(absolutePath==null || absolutePath.length()==0) return false;
        return absolutePath.endsWith(".class") || absolutePath.endsWith(".jar") ||absolutePath.endsWith(".so") ;
    }
    public static boolean isJavaClassFile(String absolutePath) {
        if(absolutePath==null || absolutePath.length()==0) return false;
        return absolutePath.endsWith(".class") || absolutePath.endsWith(".jar") ||absolutePath.endsWith(".so") ;
    }

    public static ExecutorService getExecutors() {
        return sExecutors;
    }


    public static  String getClassName(String path) {
        String[] parts = null;
        if(path.contains("intermediates/javac/debug/classes/")){
            parts = path.split("intermediates/javac/debug/classes/");
        }
        Pattern patternV3as =  Pattern.compile("intermediates/javac/\\w*Debug/classes/");
        Matcher matcherV3as = patternV3as.matcher(path);

        if(parts==null &&matcherV3as.find()){
            parts = path.split(matcherV3as.group());
        }
        if(parts==null && path.contains("intermediates/classes/debug/")){
            parts = path.split("intermediates/classes/debug/");
        }
        Pattern patternV2as =  Pattern.compile("intermediates/classes/\\w*/debug/");
        Matcher matcherV2as = patternV2as.matcher(path);

        if(parts==null &&matcherV2as.find()){
            parts = path.split(matcherV2as.group());
        }

        if(parts==null || parts.length<2){
            return null;
        }
        String className = parts[1];

        return className;
    }


    public static  String getClassPath(String path) {
        String classpath = null;
        String classpathType_1 = "intermediates/javac/debug/classes/";
        int index = path.indexOf(classpathType_1);

        if(index>=0){
            classpath = path.substring(0,index+classpathType_1.length());
        }
        Pattern patternV3as =  Pattern.compile("intermediates/javac/\\w*Debug/classes/");
        Matcher matcherV3as = patternV3as.matcher(path);

        if(classpath==null &&matcherV3as.find()){
            index = path.indexOf(matcherV3as.group());
            classpath = path.substring(0,index+matcherV3as.group().length());
        }

        String classpathType_2 = "intermediates/classes/debug/";
        index = path.indexOf(classpathType_2);
        if(classpath==null && index>=0){
            classpath = path.substring(0,index+classpathType_2.length());
        }
        Pattern patternV2as =  Pattern.compile("intermediates/classes/\\w*/debug/");
        Matcher matcherV2as = patternV2as.matcher(path);

        if(classpath==null &&matcherV2as.find()){
            index = path.indexOf(matcherV2as.group());
            classpath = path.substring(0,index+matcherV2as.group().length());
        }

        return classpath;
    }
}
